package com.qfedu.qfoffice.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.qfoffice.entity.Grade;
import com.qfedu.qfoffice.entity.ResponseData;
import com.qfedu.qfoffice.entity.User;
import com.qfedu.qfoffice.service.GradeService;
import com.qfedu.qfoffice.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("grade")
public class  GradeController {


    @Autowired
    private GradeService gradeService;

    @RequestMapping("/selectGradeAll.do")
    @ResponseBody
    public ResponseData<List<Grade>> selectGradeAll(int page, int limit) {
        //开启分页
        PageHelper.startPage(page, limit);
        List<Grade> gradeList = gradeService.selectGradeAll();
        // 得到分页相关信息
        PageInfo pageInfo = new PageInfo(gradeList);
        ResponseData<List<Grade>> responseData = new ResponseData<>();
        responseData.setCode(0);
        responseData.setMsg("success");
        responseData.setCount((int) pageInfo.getTotal());
        responseData.setData(gradeList);
        return responseData;


    }

    @RequestMapping("/deleteGradeById.do")
    @ResponseBody
    public ResponseData<Grade> deleteGradeById(int id) {

        int i = gradeService.deleteGradeById(id);
        ResponseData<Grade> responseData = new ResponseData<>();
        if (i > 0)
            responseData.setCode(1);
        return responseData;
    }

    @RequestMapping("/updateGrade.do")
    @ResponseBody
    public ResponseData<Grade> updateGrade(int id, HttpServletRequest request) {
        String name = request.getParameter("name");
        String createtime = request.getParameter("createtime");
        String weekStr = request.getParameter("week");
        String location = request.getParameter("location");
        int week = Integer.valueOf(weekStr);
        ResponseData<Grade> responseData = new ResponseData<>();
        Grade grade = new Grade();
        grade.setId(id);
        grade.setName(name);
        grade.setCreatetime(createtime);
        grade.setWeek(week);
        grade.setLocation(location);
        int i = gradeService.gradeupdate(grade);
        if (i > 0)
            responseData.setCode(1);
        return responseData;
    }

    @RequestMapping("/insertGrade.do")
    @ResponseBody
    public ResponseData<List<Grade>> insertGrade(HttpServletRequest request, Grade grade) {
//        String u_no = request.getParameter("u_no");
//        String name = request.getParameter("name");
//        String createtime = request.getParameter("createtime");
//        String weekStr = request.getParameter("week");
//        String location = request.getParameter("location");
//        int week = Integer.valueOf(weekStr);
//        ResponseData<Grade> responseData = new ResponseData<>();
//        Grade grade = new Grade();
//        grade.setId(id);
//        grade.setU_no(u_no);
//        grade.setName(name);
//        grade.setCreatetime(createtime);
//        grade.setWeek(week);
//        grade.setLocation(location);
        User user = (User) request.getSession().getAttribute("user");
        String no = user.getNo();
        grade.setUno(no);
        String name = request.getParameter("name");
        int week = Integer.valueOf(request.getParameter("week"));
        String createtime = request.getParameter("createtime");
        String location = request.getParameter("location");
        grade.setName(name);
        grade.setCreatetime(createtime);
        grade.setWeek(week);
        grade.setLocation(location);
        ResponseData<List<Grade>> responseData = new ResponseData<>();
        int num = gradeService.insertGrade(no, name, createtime, week, location);
        if (num > 0) {
            responseData.setCode(1);
        }else{
            responseData.setCode(0);
        }
        return responseData;
    }
}
